package com.jingyanzi.member.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.jingyanzi.member.domain.SchedulingDO;
import com.jingyanzi.member.domain.result.ResultDO;
import com.jingyanzi.member.domain.result.ResultSupport;
import com.jingyanzi.member.query.SchedulingQueryDO;
import com.jingyanzi.member.response.PageResponse;
import com.jingyanzi.member.service.ISchedulingService;
import com.jingyanzi.resallocation.domain.SingleRequest;
import com.jingyanzi.resallocation.result.SingleResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "人员排期controller", tags = { "人员排期接口" })
@RestController
@RequestMapping("/allocation")
public class SchedulingController {

	@Autowired
	ISchedulingService schedulingService;

	@ApiOperation(value = "创建人员排期", notes = "创建人员排期")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "创建人员排期(从员工列表里面选择)", required = true, dataType = "SingleRequest<SchedulingDO>") })
	@RequestMapping(value = "/add.json", method = RequestMethod.POST)
	public SingleResponse<SchedulingDO> add(@RequestBody SingleRequest<SchedulingDO> request) {

		SchedulingDO domain = request.getData();

		ResultDO result = schedulingService.add(domain);
		SingleResponse<SchedulingDO> response = new SingleResponse<SchedulingDO>();
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("添加成功");
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("添加失败");
		}
		return response;
	}

	@ApiOperation(value = "修改人员排期信息", notes = "修改人员排期信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "人员排期对象(修改时从员工列表里面选择)", required = true, dataType = "SingleRequest<SchedulingDO>") })
	@RequestMapping(value = "/edit.json", method = RequestMethod.POST)
	public SingleResponse<SchedulingDO> edit(@RequestBody SingleRequest<SchedulingDO> request) {

		SchedulingDO domain = request.getData();
		System.out.println(domain);
		ResultDO result = schedulingService.modifi(domain);
		SingleResponse<SchedulingDO> response = new SingleResponse<SchedulingDO>();
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("修改成功");
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("修改失败");
		}
		return response;
	}

	@ApiOperation(value = "获取人员排期详情", notes = "获取人员排期详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "人员排期对象(只传客户的ID)", required = true, dataType = "SingleRequest<SchedulingDO>") })
	@RequestMapping(value = "/detail.json", method = RequestMethod.POST)
	public SingleResponse<SchedulingDO> detail(@RequestBody SingleRequest<SchedulingDO> request) {

		SchedulingDO domain = request.getData();

		ResultDO result = schedulingService.get(domain.getId());
		SingleResponse<SchedulingDO> response = new SingleResponse<SchedulingDO>();
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("获取成功");
			response.setData((SchedulingDO) result.getModel(ResultSupport.FIRST_MODEL_KEY));
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("获取失败");
		}
		return response;
	}

	@ApiOperation(value = "获取人员排期信息", notes = "获取人员排期信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "获取人员排期对象(只传客户的ID)", required = true, dataType = "SingleRequest<SchedulingDO>") })
	@RequestMapping(value = "/delete.json", method = RequestMethod.POST)
	public SingleResponse<SchedulingDO> delete(@RequestBody SingleRequest<SchedulingDO> request) {

		SchedulingDO domain = request.getData();

		ResultDO result = schedulingService.remove(domain.getIds());
		SingleResponse<SchedulingDO> response = new SingleResponse<SchedulingDO>();
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("删除成功");
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("删除失败");
		}
		return response;
	}

	@ApiOperation(value = "查询人员排期信息", notes = "查询人员排期信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "客户查询对象", required = true, dataType = "SingleRequest<CustomerQueryDO>") })
	@RequestMapping(value = "/query.json", method = RequestMethod.POST)
	public PageResponse<SchedulingDO> query(@RequestBody SingleRequest<SchedulingQueryDO> request) {

		SchedulingQueryDO query = request.getData();
		if (query.getPageRows() > 0) {
			query.setPage(true);
		}
		ResultDO result = schedulingService.query(query);

		PageResponse<SchedulingDO> response = new PageResponse<SchedulingDO>();
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("查询成功");
			response.setData((List<SchedulingDO>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			if(query.isPage()) {
				response.setTotal((Long) result.getModel(ResultSupport.SECOND_MODEL_KEY));
			}
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("查询失败");
		}
		return response;
	}
}
